const axios = require('axios').default
const gbk = require('gbk')

async function loginAndCheck (Cookie) {
  await axios.get(`http://bbs.ra2ol.com/home.php`, {
    headers: {
      Cookie
    }
  })
  return axios.get(`http://bbs.ra2ol.com/home.php?mod=spacecp&ac=credit&op=base`, {
    headers: {
      Cookie
    },
    responseType: 'arraybuffer',
    transformResponse: [
      (data) => {
        return gbk.toString('utf-8', data)
      }
    ]
  })
}

function findData (html) {
  const prefix = '黄金: </em>'
  const start = html.indexOf(prefix)
  const end = html.indexOf(' 两')

  return html.substring(start + prefix.length, end)
}

module.exports = {
  loginAndCheck,
  findData
}
